package us.icitap.commandcenter.ui.supervisor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallManagement;
import us.icitap.commandcenter.business.ejb.persistence.CallReason;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.business.ejb.persistence.PoliceAction;
import us.icitap.commandcenter.listeners.CloseVerificationListener;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustComboBox;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.ui.custom.NotificationTextField;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.commandcenter.utils.ProjectLists;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;

public class CloseVerificationWindow extends Window implements ClickListener {

	private Call call;
	
	private CloseVerificationListener closeVerificationListener;
	
	BeanFieldGroup<Call> binder = new BeanFieldGroup<Call>(Call.class);

	private final NotificationTextField notificationTextField = new NotificationTextField("Kush njoftohet per telefonaten");
	private final CustTextArea verificationNotesTextArea = new CustTextArea("Shenime mbi verifikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");

	public CloseVerificationWindow(CloseVerificationListener closeVerificationListener, Call call) {
		
		this.closeVerificationListener = closeVerificationListener;
		this.setCall(call);
		
		
		this.setCaption("Te dhena shtese per telefonaten");
		this.setModal(true);
		this.center();
		this.setResizable(false);
		
		binder.bind(verificationNotesTextArea, "verificationNotes");

		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		vl.setSpacing(true);
		
	
		
		this.notificationTextField.setWidth("450px");
		this.notificationTextField.setImmediate(true);
		vl.addComponent(notificationTextField);
		
	
		this.verificationNotesTextArea.setWidth("450px");
		this.verificationNotesTextArea.setImmediate(true);
		vl.addComponent(verificationNotesTextArea);


		final ButtonsLayout buttonsLayout = new ButtonsLayout("600px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		
		this.setContent(vl);
		
		this.saveButton.addClickListener(this);

	}
	
	public void setCall(Call call){
		this.call = call;
		for (Field<?> field : binder.getFields()) {
			   field.removeAllValidators();
		}
		binder.setItemDataSource(this.call);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			notificationTextField.commit();
			binder.commit();

			if(notificationTextField.getValue() != null){
				CallManagement callManagement= new CallManagement(call);
				callManagement.setWhoIsNotified(notificationTextField.getValue());
				callManagement.setRecordDate(new Date());
				callManagement.setRecordUser(CommandCenterUI.getUser().getUsername());
				if(call.getCallManagementList() == null) call.setCallManagementList(new ArrayList<CallManagement>());
				call.getCallManagementList().add(callManagement);
			}
			

			call.setActualStatus(CallStatusEnum.V);
			call.setVerificationDate(new Date());
			call.setVerificationUser(CommandCenterUI.getUser().getUsername());
			
			this.closeVerificationListener.closeCallVerification(call);
			
		} catch (Exception e) {
			HandleNotification.showWarningOnScreen(e.getCause().getMessage());
		}
		
	}
	
}
